package com.fr.base.core.json;

import com.fr.base.CodeUtils;
import com.fr.base.StringUtils;
import com.fr.report.script.Primitive;
import com.fr.report.script.core.FArray;
import com.fr.web.core.service.AttachmentService;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class JSONUtils
{
  public static Object quoteFrObject(Object paramObject)
  {
    if (paramObject instanceof String)
      return JSONObject.quote((String)paramObject);
    if (paramObject instanceof FArray)
    {
      FArray localFArray1 = (FArray)paramObject;
      FArray localFArray2 = new FArray();
      for (int i = 0; i < localFArray1.length(); ++i)
        localFArray2.add(quoteFrObject(localFArray1.elementAt(i)));
      return "[" + localFArray2 + "]";
    }
    return paramObject;
  }

  public static Map jsonString2Map(String paramString)
    throws JSONException
  {
    HashMap localHashMap = new HashMap();
    if (StringUtils.isNotBlank(paramString))
    {
      JSONObject localJSONObject = new JSONObject(paramString);
      Iterator localIterator = localJSONObject.keys();
      while (localIterator.hasNext())
      {
        Object localObject1 = localIterator.next();
        Object localObject2 = localJSONObject.get(localObject1.toString());
        if (localObject2 instanceof String)
        {
          localObject2 = CodeUtils.carlDecode((String)localObject2);
        }
        else if (localObject2 instanceof JSONArray)
        {
          JSONArray localJSONArray = (JSONArray)localObject2;
          FArray localFArray = new FArray();
          for (int i = 0; i < localJSONArray.length(); ++i)
            localFArray.add(localJSONArray.get(i));
          localObject2 = localFArray;
        }
        localHashMap.put(localObject1, localObject2);
      }
    }
    return ((Map)localHashMap);
  }

  public static Object objectDecode(Object paramObject)
    throws JSONException
  {
    Object localObject1 = paramObject;
    if (paramObject instanceof JSONObject)
    {
      Object localObject2;
      JSONObject localJSONObject = (JSONObject)paramObject;
      if (localJSONObject.has("__time__"))
      {
        localObject2 = localJSONObject.get("__time__");
        if (localObject2 instanceof Long)
          localObject1 = new Date(((Long)localObject2).longValue());
      }
      else if (localJSONObject.has("attach_id"))
      {
        localObject2 = localJSONObject.get("attach_id");
        if (localObject2 instanceof String)
          localObject1 = AttachmentService.getAttachment((String)localObject2);
      }
    }
    else if (JSONObject.NULL.equals(paramObject))
    {
      localObject1 = Primitive.NULL;
    }
    return localObject1;
  }
}